package com.ktian.activity;

import java.io.File;

import com.xutils.http.utils.HttpUtils;
import com.xutils.http.RequestParams;
import com.xutils.http.ResponseInfo;
import com.xutils.http.callback.RequestCallBack;
import com.xutils.http.client.HttpRequest.HttpMethod;
import com.xutils.http.exception.HttpException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		HttpUtils httpUtils = new HttpUtils();
		
		// 文件下载
		httpUtils
				.download(
						"http://apps.lidroid.com/apiv2/dl/0000000/com.lidroid.fileexplorer", // 下载地址
						Environment.getExternalStorageDirectory().getPath()
								+ "ktian" + System.currentTimeMillis()
								+ "/ko.html", // 存放的本地地址,包含文件后缀
						new RequestCallBack<File>() {

							@Override
							public void onLoading(long total, long current,
									boolean isUploading) {
								// TODO Auto-generated method stub
								Log.d("net", "current-->" + current);
								Log.d("net", "total-->" + total);
								Log.d("net", "-->" + current * 100 / total
										+ "%");
							}

							@Override
							public void onSuccess(
									ResponseInfo<File> responseInfo) {
								// TODO Auto-generated method stub
								Log.d("000", "downloaded:"
										+ responseInfo.result.getPath());
							}

							@Override
							public void onFailure(HttpException error,
									String msg) {
								Log.d("net", "onFailure--" + msg);
							}

						});
		
		// 接口调用
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", "value");		// 参数，头部信息，文件等都可在这里添加
		
		httpUtils
				.send(HttpMethod.GET,
						"http://apps.lidroid.com/apiv2/dl/0000000/com.lidroid.fileexplorer", // 接口地址
						new RequestCallBack<String>() {

							@Override
							public void onLoading(long total, long current,
									boolean isUploading) {

							}

							@Override
							public void onSuccess(
									ResponseInfo<String> responseInfo) {
								Log.d("net", "onSuccess-->"
										+ responseInfo.result); // 返回的结果
							}

							@Override
							public void onFailure(HttpException error,
									String msg) {
								Log.d("net", "onFailure");
							}

							@Override
							public void onFinish() {
								
							}

						});
	}

	

}
